package com.aj.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.Family;
import com.aj.databases.entry.FamilyMember;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.ManageFamilyRequest;
import com.aj.http.request.QueryFamilyRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.QueryFamilyResponse;
import com.aj.utils.PortraitLoaderUtil;

public class ManageFamilyInfoActivity extends BaseActivity {
	private TextView enter_lovehomenumber;
	private TextView enter_telphone;
	private TextView enter_description;
	private TextView text_sex;
	private TextView enter_birth;
	private TextView enter_age;
	private TextView enter_sign;
	private Button sure_button;
	private Button cannel_button;
	private String isPravite = "1";
	private UserDao userDao;
	private User user;
	private ImageView header;
	private TextView lovenumber_text;
	private String callId = "1";
	private String callName = "爸爸";
	private Family family = null;
	private FamilyMember currentFamilyMember = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_manager_family);
		setActionBarTitle("管理家人");
		initView();
	}

	@Override
	protected void onResume() {
		super.onResume();
		new QueryFamilyRequest().excute(ManageFamilyInfoActivity.this,
				user.getUser_id(), family.getUserid(), new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {
							QueryFamilyResponse queryFamilyResponse = new QueryFamilyResponse(
									new JSONObject(result));
							if (queryFamilyResponse.getCommon() != null
									&& queryFamilyResponse
											.getCommon()
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								currentFamilyMember = queryFamilyResponse
										.getFamilyMember();
								refreshView();
							} else {
								Toast.makeText(
										ManageFamilyInfoActivity.this,
										queryFamilyResponse.getCommon()
												.getErrorMsg(),
										Toast.LENGTH_LONG).show();
							}
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}

					@Override
					public void fail(String result) {
						try {
							Common common = new Common(new JSONObject(result));
							Toast.makeText(ManageFamilyInfoActivity.this,
									common.getErrorMsg(), Toast.LENGTH_LONG)
									.show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
					}
				});
	}

	private void refreshView() {
		PortraitLoaderUtil.load(ManageFamilyInfoActivity.this, header, null,
				currentFamilyMember.getPhoto(), R.drawable.head_icon,
				R.drawable.head_icon);
		lovenumber_text.setText("爱家号：" + currentFamilyMember.getAjNo());
		enter_lovehomenumber.setText(currentFamilyMember.getTrueName());
		enter_telphone.setText(currentFamilyMember.getNickName());
		text_sex.setText(currentFamilyMember.getSix().equals("0") ? "男" : "女");
		enter_birth.setText(currentFamilyMember.getBirthday());
		enter_age.setText(currentFamilyMember.getAge());
		enter_sign.setText(currentFamilyMember.getSign());
		if (callName.equals("爸爸") || callName.equals("妈妈")) {
			sure_button.setText("解除关系");
			cannel_button.setText("设为私有");
		} else if (callName.equals("岳父") || callName.equals("岳母")) {
			sure_button.setVisibility(View.GONE);
			cannel_button.setVisibility(View.GONE);
		} else if (callName.equals("老婆")) {
			sure_button.setText("离婚");
			cannel_button.setVisibility(View.GONE);
		}
	}

	private void initView() {
		enter_lovehomenumber = (TextView) findViewById(R.id.enter_lovehomenumber);
		enter_telphone = (TextView) findViewById(R.id.enter_telphone);
		enter_description = (TextView) findViewById(R.id.enter_description);
		text_sex = (TextView) findViewById(R.id.text_sex);
		header = (ImageView) findViewById(R.id.header);
		enter_birth = (TextView) findViewById(R.id.enter_birth);
		lovenumber_text = (TextView) findViewById(R.id.lovenumber_text);
		enter_age = (TextView) findViewById(R.id.enter_age);
		enter_sign = (TextView) findViewById(R.id.enter_sign);
		callName = getIntent().getStringExtra("callName");
		family = (Family) getIntent().getSerializableExtra("family");
		if (callName != null) {
			if (callName.equals("爸爸")) {
				callId = "1";
			} else if (callName.equals("妈妈")) {
				callId = "2";
			} else if (callName.equals("儿子")) {
				callId = "7";
			} else if (callName.equals("女儿")) {
				callId = "8";
			} else if (callName.equals("胎儿")) {
				callId = "13";
			}
		}
		userDao = new UserDao(this);
		user = userDao.getCurrentUser(LoveHomeApplication.token);
		ButtonClick buttonClick = new ButtonClick();
		sure_button = (Button) findViewById(R.id.sure_button);
		cannel_button = (Button) findViewById(R.id.cannel_button);
		sure_button.setOnClickListener(buttonClick);
		cannel_button.setOnClickListener(buttonClick);
	}

	class ButtonClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.sure_button:
				if (callName.equals("爸爸") || callName.equals("妈妈")) {
					new ManageFamilyRequest().excute(
							ManageFamilyInfoActivity.this, user.getUser_id(),
							family.getUserid(), "2", new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										Common common = new Common(
												new JSONObject(result));
										if (common.getReturnCode() != null
												&& common
														.getReturnCode()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
											finish();
										}
										Toast.makeText(
												ManageFamilyInfoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}

								@Override
								public void fail(String result) {
									try {
										Common common = new Common(
												new JSONObject(result));
										Toast.makeText(
												ManageFamilyInfoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});
				} else if (callName.equals("老婆")) {

				}
				break;
			case R.id.cannel_button:
				if (callName.equals("爸爸") || callName.equals("妈妈")) {

					new ManageFamilyRequest().excute(
							ManageFamilyInfoActivity.this, user.getUser_id(),
							family.getUserid(), "3", new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										Common common = new Common(
												new JSONObject(result));
										if (common.getReturnCode() != null
												&& common
														.getReturnCode()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
											finish();
										}
										Toast.makeText(
												ManageFamilyInfoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}

								@Override
								public void fail(String result) {
									try {
										Common common = new Common(
												new JSONObject(result));
										Toast.makeText(
												ManageFamilyInfoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});

				}
				finish();
				break;
			}
		}
	}
}
